package topo.table;

import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class ObjectTableModel extends DefaultTableModel {
    static final String idName = ".ObjectIDName.";
    private ObjectTableDesc desc;

    public ObjectTableModel(ObjectTableDesc theDesc) {
        this(toColNames(theDesc));
        this.desc = theDesc;
    }

    public void addRowObject(Object obj) {
        addRowObject(obj, this.desc.getRowDatas(obj));
    }

    public void addRowObject(List list) {
        for (int i = 0; i < list.size(); i++) {
            addRowObject(list.get(i));
        }
    }

    public void addRowObject(Enumeration enu) {
        while (enu.hasMoreElements()) {
            addRowObject(enu.nextElement());
        }
    }

    public void delRowObject(Object obj) {
        int index = getRowObjectIndex(obj);
        if (index >= 0) {
            super.removeRow(index);
        }
    }

    public void delRowObject(List list) {
        for (int i = 0; i < list.size(); i++) {
            delRowObject(list.get(i));
        }
    }

    public void updateRowObject(Object obj) {
        updateRowObject(obj, this.desc.getRowDatas(obj));
    }

    public void clearAllObject() {
        this.dataVector.clear();
        fireTableDataChanged();
    }

    public Object getRowObject(int row) {
        int colLen = super.getColumnCount();

        return super.getValueAt(row, colLen - 1);
    }

    public Vector getAllRowObject() {
        Vector vec = new Vector();
        int rowLen = super.getRowCount();

        for (int i = 0; i < rowLen; i++) {
            Object obj = getRowObject(i);
            vec.add(obj);
        }
        return vec;
    }

    private static Object[] toColNames(ObjectTableDesc theDesc) {
        Object[] viCols = theDesc.getcolumnNames();
        Object[] cols = new Object[viCols.length + 1];
        System.arraycopy(viCols, 0, cols, 0, viCols.length);
        cols[(cols.length - 1)] = ".ObjectIDName.";
        return cols;
    }

    private ObjectTableModel(Object[] columnNames) {
        super(columnNames, 0);
    }

    public boolean isCellEditable(int row, int col) {
        return false;
    }

    private void addRowObject(Object obj, Object[] rowViewDatas) {
        if ((obj == null) || (rowViewDatas == null)) {
            return;
        }
        int index = getRowObjectIndex(obj);
        if (index >= 0) {
            updateRowObject(obj, rowViewDatas);
        } else {
            Object[] datas = toRowDatas(obj, rowViewDatas);
            super.addRow(datas);
        }
    }

    private void updateRowObject(Object obj, Object[] rowViewDatas) {
        if (obj == null) {
            return;
        }
        int index = getRowObjectIndex(obj);
        if (index >= 0) {
            Object[] rowDatas = toRowDatas(obj, rowViewDatas);
            for (int col = 0; col < rowDatas.length; col++) {
                setValueAt(rowDatas[col], index, col);
            }
        }
    }

    private int getRowObjectIndex(Object obj) {
        if (obj == null) {
            return -1;
        }
        int rowLen = super.getRowCount();
        for (int i = 0; i < rowLen; i++) {
            Object existObj = getRowObject(i);
            if ((existObj != null) && (existObj.equals(obj))) {
                return i;
            }
        }

        return -1;
    }

    private Object[] toRowDatas(Object obj, Object[] rowViewDatas) {
        Object[] datas = new Object[rowViewDatas.length + 1];
        System.arraycopy(rowViewDatas, 0, datas, 0, rowViewDatas.length);
        datas[(datas.length - 1)] = obj;
        return datas;
    }
}